<?php
class UserController extends Zend_Controller_Action
{
	public function init()
	{
		IndexController::initAdmin($this);
		
		$this->_arrKeyUser	= array('country', 'group_id', 'admin_firstname', 'admin_lastname', 'admin_username', 'admin_isactive');
	}
	
	private function _isAdministrator()
	{
		if($this->AppUI->admin_group_id!=GROUP_Administrator)
		{
			header('Location:'.HOST_ADMIN);
			exit;
		}
	}
	
	public function indexAction()
	{
		$this->_isAdministrator();
		
		$intGroupId	= $this->_request->getParam('g', 0);
		$intCountryId	= $this->_request->getParam('country', null);
		$email	= $this->_request->getParam('email', null);
		$arrResult	= QAdminUser::search($intCountryId, $intGroupId, $email);
		
		$this->view->intGroupId	= $intGroupId;
		$this->view->intCountryId	= $intCountryId;
		$this->view->email	= $email;
		$this->view->arrResult	= $arrResult;
		$this->view->arrTeam	= CConfig::team_list();
		$this->view->arrCountry = CConfig::country_list();
	}
	
	#Sale Data
	public function saveAction()
	{
		$objFile	= isset($_FILES['file']) ? $_FILES['file'] : null ;
		if(!$objFile['name'])
		{
			//echo '<script>parent.show_error("saledata", "null")</script>';
			exit;
		}
		if($objFile)
		{
			$strDelimiter	= ",";
			
			$arrKey			= $this->_arrKeyUser;
			$db				= Zend_Registry::get('db');
			$strQuery		= "TRUNCATE TABLE admins_temp; LOAD DATA LOCAL INFILE '".str_replace('\\', '/', $objFile['tmp_name'])."' 
								INTO TABLE admins_temp
								FIELDS TERMINATED BY ',' ENCLOSED BY '\"'
								LINES TERMINATED BY '\r\n'
								IGNORE 1 LINES (".implode(',', $arrKey).")";
			$db		= Zend_Registry::get('db');
			$db->execute($strQuery);
			
			//get data from admins_temp insert to admins
			$arrTemp = QAdminUser::get_admins_temp();
			if($arrTemp)
			{
				$objAdmin = new QAdminUser();				
				foreach ($arrTemp as $item)
				{
					$p_Params = array(
						'admin_username'	=> $item['admin_username'],
						'admin_firstname'	=> $item['admin_firstname'],
						'admin_pwd'			=> '',
						'admin_lastname'	=> $item['admin_lastname'],
						'group_id'			=> constant($item['group_id']),
						'admin_isactive'	=> $item['admin_isactive'],
						'email'				=> $item['admin_username'],
						'country'			=> CConfig::getCountryId($item['country'])
					);
				
					$arrItem = QAdminUser::getbyUsername($item['admin_username']);
					if(!$arrItem)
					{
						$result = $objAdmin->insert($p_Params);
					}
				}
			}
			
			QAdminUser::updateUpload();
			
			echo '<script>parent.update_done();</script>' . $objFile;
			exit;
		}
		exit;
	}
	
	public function addAction()
	{
		$this->_isAdministrator();
		
		if($this->_request->isPost())
		{
			$intAdminId		= $this->_request->getParam('intAdminId', null);
			$strFirstname	= $this->_request->getParam('strFirstname', null);
			$strLastname	= $this->_request->getParam('strLastname', null);
			$strUsername	= $this->_request->getParam('strUsername', null);
			$strPassword	= $this->_request->getParam('strPassword', null);
			$strPasswordNew	= $this->_request->getParam('strPasswordNew', null);
			$intGroupId		= $this->_request->getParam('intGroupId', null);
			$intGroupLeader	= $this->_request->getParam('intGroupLeader', null);
			$strCategory	= $this->_request->getParam('strCategory', null);
			$strEmail		= $strUsername;
			$intActive		= $this->_request->getParam('intActive', 0);
			
			$arrInfo		= QAdminUser::search(null, null, $strUsername);
			
			if($arrInfo && $arrInfo[0]['admin_id']!=$intAdminId)
			{
				echo '<script>parent.show_error("strUsername", "exists")</script>';
				exit;
			}
			
			if(!$intAdminId)
			{
				$arrParams	= array
				(
					'admin_username'	=> $strUsername,
					'admin_pwd'			=> md5($strPassword),
					'admin_firstname'	=> $strFirstname,
					'admin_lastname'	=> $strLastname,
					'email'				=> $strEmail,
					'group_id'			=> $intGroupId,
					'group_leader'		=> $intGroupLeader,
					'category'			=> $strCategory,
					'admin_isactive'	=> $intActive,
					'admin_createdate'	=> date('Y-m-d H:i:s'),
					'country'			=> $this->AppUI->country
				);
				
				QAdminUser::insert($arrParams);
				
				$objAdminDragonTool = new QAdminUserDragonTool();
				$result = $objAdminDragonTool->insert($arrParams);
				
				echo '<script>parent.submit_done()</script>';
			}
			else
			{
				$arrParams		= array
				(
					'admin_firstname'	=> $strFirstname,
					'admin_lastname'	=> $strLastname,
					'admin_pwd'			=> $strPasswordNew ? md5($strPasswordNew) : $strPassword ,
					'group_id'			=> $intGroupId,
					'group_leader'		=> $intGroupLeader,
					'category'			=> $strCategory,
					'email'				=> $strEmail,
					'admin_isactive'	=> $intActive,
					'admin_username'	=> $strUsername
				);
				QAdminUser::update($arrParams, $intAdminId);
				echo '<script>parent.submit_done()</script>';
			}
			exit;
		}
		
		$intAdminId	= $this->_request->getParam('i');
		if($intAdminId)
		{
			$arrResult	= QAdminUser::get_by_id($intAdminId);
			$this->view->arrResult	= $arrResult;
		}
		$this->view->arrTeam	= CConfig::team_list();
		$this->view->arrCountry = CConfig::country_list();
	}
	
	public function changepwdAction()
	{
		$AppUI 			= Zend_Registry::get('AppUI');
		$strPassOld 	= $this->_request->getParam('txtPassOld','');
		$strPassNew 	= $this->_request->getParam('txtPassNew','');
		$strRePassNew 	= $this->_request->getParam('txtRePassNew','');
		$bolResult 		= true;
		if ($strPassOld && $strPassNew && $strRePassNew)
		{
			if ($strPassNew == $strRePassNew)
			{
				$bolResult = CAdminUser::updatePassword($strPassOld, $strPassNew);
				if ($bolResult)
				{
					$this->_redirect(HOST_ADMIN.'/logout');
				}
			}
			else
			{
				$this->view->bolErrorPass = true;
			}
		}
		$this->view->bolResult 		= $bolResult;
		$this->view->strUsername 	= $AppUI->admin_username;
	}
	
	public function changepwdUserAction()
	{
		$AppUI = Zend_Registry::get('AppUI');
		if($AppUI->admin_id!=ADMINISTRATOR)
		{
			$this->_redirect(HOST_ADMIN_ERROR);
		}
		
		if(	$this->_request->isPost() )
		{
			$strUsername= $this->_request->getParam('strUsername', null);
			$strPwdOld	= $this->_request->getParam('strPwdOld', null);
			$strPwd		= $this->_request->getParam('strPwd', null);
			
			QAdminUser::updatePassword($strUsername, $strPwdOld, md5($strPwd));
			
			header('Location:'.HOST_ADMIN.'/user');
			exit;
		}
		
		$intAdminId	= $this->_request->getParam('i', null);
		$arrInfo	= CAdminUser::get(0, 0, $intAdminId);
		$this->view->arrInfo = $arrInfo[0];
	}
}
?>
